/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
using FlickrNet;

namespace ArtNotes
{
	/// <summary>
	/// Shows the metadata gathered from Flickr
	/// about a specific image.
	/// </summary>
	public class ANFlickrInfo: UIScrollView
	{

		private ANDatabase anDB;
		private Photo photo;
		private FlickrImage flickrImage;
		
		private UITextField tfTitle;
		private UITextField tfAuthor;
		private UITextField tfLattitude;
		private UITextField tfLongitude;
		private UITextField tfLicense;
		private UITextField tfPhotoID;
		
		private UITextView tvDescription;
		
		private UILabel lTitle;
		private UILabel lAuthor;
		private UILabel lLattitude;
		private UILabel lLongitude;
		private UILabel lDescription;
		private UILabel lLicense;
		private UILabel lPhotoID;
		
		private UIImageView tbTitle;
		private UIImageView tbAuthor;
		private UIImageView tbLattitude;
		private UIImageView tbLongitude;
		private UIImageView taDescription;
		private UIImageView tbLicense;
		private UIImageView tbPhotoID;
		
		public ANFlickrInfo (Photo photo)
		{
			this.photo = photo;
			init(true);
		}
		
		public ANFlickrInfo (FlickrImage img)
		{
			this.flickrImage = img;
			init(false);
		}
		
		private void init(bool isFromWeb)
		{
			this.anDB = AN_APP_OPTIONS.AN_DATABASE;
			
			this.ContentSize = new SizeF(320f,470f);
			this.Frame = new RectangleF (0, 0, 320, 300);
			this.ClipsToBounds = true;
			this.ScrollEnabled = true;
			this.AlwaysBounceVertical = true;
			this.Bounds = this.Frame;
			this.ShowsVerticalScrollIndicator = true;
			this.DirectionalLockEnabled = true;
			
			this.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			
			UIImageView flickrLogo = new UIImageView(new RectangleF(80,10,160,50));
			flickrLogo.Image = UIImage.FromFile("Images/flickr_penned.png");
			
			lTitle = createLabel("Title",new PointF(10,80),new SizeF(50,20));
			tbTitle = createTextBoxOutline(new PointF(120,70),new SizeF(200,40));
			tfTitle = createInputItem(new PointF(130,80));
			if(flickrImage.Title != null)
				tfTitle.Text = flickrImage.Title;
			
			lAuthor = createLabel("Author",new PointF(10,110),new SizeF(60,20));
			tbAuthor = createTextBoxOutline(new PointF(120,100),new SizeF(200,40));
			tfAuthor = createInputItem(new PointF(130,110));
			if(flickrImage.OwnerName != null)
				tfAuthor.Text = flickrImage.OwnerName;
			
			lPhotoID = createLabel("Photo ID",new PointF(10,140),new SizeF(70,20));
			tbPhotoID = createTextBoxOutline(new PointF(120,130),new SizeF(200,40));
			tfPhotoID = createInputItem(new PointF(130,140));
			if(flickrImage.PhotoID != null)
				tfTitle.Text = flickrImage.PhotoID;
			
			lLicense = createLabel("License",new PointF(10,170),new SizeF(60,20));
			tbLicense = createTextBoxOutline(new PointF(120,160),new SizeF(200,40));
			tfLicense = createInputItem(new PointF(130,170));
			if(flickrImage.License != null)
				tfLicense.Text = flickrImage.License;
			
			lDescription = createLabel("Description",new PointF(10,200),new SizeF(100,20));
			taDescription = new UIImageView(new RectangleF(new PointF(10,220),new SizeF(300,110)));
			taDescription.Image = UIImage.FromFile("Images/text_area_grey.png");
			taDescription.BackgroundColor = UIColor.Clear;
			//taDescription = createTextBoxOutline(new PointF(10,90),new SizeF(200,40));
			tvDescription = new UITextView();
			tvDescription.Frame = new RectangleF(20,220,280,90);
			tvDescription.BackgroundColor = UIColor.Clear;
			tvDescription.Font = UIFont.FromName ("Marker Felt", 18);
			tvDescription.TextColor = UIColor.DarkGray;
			if(flickrImage.Description != null)
				tvDescription.Text = flickrImage.Description;
			
			// The stupidity is strong in this one.
			tvDescription.Started += delegate {
				PointF dmf = this.ContentOffset;
				UIButton dismissItem = ImageButtonType(new RectangleF(260,dmf.Y + 10,55,58), UIImage.FromFile("Images/close_x.png"),UIImage.FromFile("Images/close_x.png"));//RoundedButtonType(200,10,"Done!");
				dismissItem.TouchDown += delegate {
					//Console.WriteLine(tvDescription.Text);
					tvDescription.ResignFirstResponder ();
					dismissItem.RemoveFromSuperview();	
					dismissItem = null;
				};
			
				this.AddSubview(dismissItem);
			};
			
			lLongitude = createLabel("Longitude",new PointF(10,340),new SizeF(80,20));
			tbLongitude = createTextBoxOutline(new PointF(120,330),new SizeF(200,40));
			tfLongitude = createInputItem(new PointF(130,340));
			if(!flickrImage.Longitude.Equals(null))
				tfLongitude.Text = flickrImage.Longitude.ToString();
			
			lLattitude = createLabel("Lattitude",new PointF(10,370),new SizeF(80,20));
			tbLattitude = createTextBoxOutline(new PointF(120,360),new SizeF(200,40));
			tfLattitude = createInputItem(new PointF(130,370));
			if(!flickrImage.Lattitude.Equals(null))
				tfLattitude.Text = flickrImage.Lattitude.ToString();

			this.AddSubview(flickrLogo);
			
			this.AddSubview(tbTitle);
			this.AddSubview(tbAuthor);
			this.AddSubview(tbPhotoID);
			this.AddSubview(tbLicense);
			this.AddSubview(taDescription);
			this.AddSubview(tbLattitude);
			this.AddSubview(tbLongitude);
			
			this.AddSubview(tfTitle);
			this.AddSubview(tfAuthor);
			this.AddSubview(tfPhotoID);
			this.AddSubview(tfLicense);			
			this.AddSubview(tvDescription);
			this.AddSubview(tfLattitude);
			this.AddSubview(tfLongitude);
			
			this.AddSubview(lTitle);
			this.AddSubview(lAuthor);
			this.AddSubview(lDescription);
			this.AddSubview(lPhotoID);
			this.AddSubview(lLicense);
			this.AddSubview(lLattitude);
			this.AddSubview(lLongitude);
			
			if(isFromWeb)
			{
				//set all editing boxes to read only...
			}
		}
		
		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			return button;
		}
		
		private UIButton RoundedButtonType (float xpos, float ypos, string btitle)
		{
			var button = UIButton.FromType (UIButtonType.RoundedRect);
			button.Frame = new RectangleF (xpos, ypos, 106f, 40f);
			button.BackgroundColor = UIColor.Clear;
			button.SetTitle (btitle, UIControlState.Normal);
			button.SetTitleColor (UIColor.Black, UIControlState.Normal);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		private UIImageView createTextBoxOutline(PointF location, SizeF size)
		{
			UIImageView tmp = new UIImageView(new RectangleF(location,size));
			tmp.Image = UIImage.FromFile("Images/text_box_grey.png");
			tmp.BackgroundColor = UIColor.Clear;
			return tmp;
		}
		
		private UILabel createLabel(string title, PointF location, SizeF size)
		{
			UILabel tmp = new UILabel(new RectangleF(location,size));
			tmp.Font = UIFont.FromName ("Marker Felt", 20);
			tmp.BackgroundColor = UIColor.Clear;
			tmp.Text = title;
			return tmp;
		}
		
		private UITextField createInputItem(PointF location)
		{
			UITextField tmp = new UITextField(new RectangleF (location,new SizeF(180f,25f)));
			tmp.BorderStyle = UITextBorderStyle.None;//RoundedRect;
			tmp.BackgroundColor = UIColor.Clear;
			tmp.Font = UIFont.FromName ("Marker Felt", 18);
			tmp.TextColor = UIColor.DarkGray;
			tmp.ShouldReturn = editFieldInputHandler;
			return tmp;
		}
		
		bool editFieldInputHandler(UITextField tf)
		{
			tf.ResignFirstResponder();
			return true;
		}
	}
}
